$(window).load(function(){	
	
	//https://instagram.com/oauth/authorize/?client_id=68a296e5a7c643a98af8a0b7ee7fa68e&redirect_uri=http://localhostI&response_type=token
	var insta_next_url = "";
	var cont = 0;
	var creator = new Creator();
	var instragramContainer = $(".photosContainer");
	var firstTime = true;
	
	var generalTwitterUrl = "https://twitter.com/intent/tweet?text=Compartido%20desde%20el%20fid%2013%20#url#";
	
	var timer = setInterval(loadFeedsAutomatic,60000);
	
	FB.init({appId: "110706859117497", status: true, cookie: true});
	
	$(document).bind('cbox_complete', function(){		
		$("#compartirTwitter").attr("href",generarUrl());
	});
	
	function generarUrl(){
		
		var url = $(".cboxPhoto").attr("src");		
		var result = generalTwitterUrl.replace("#url#",url);
		return result;
	};
	
	$("#facebookButton").live("click",function(){
		
		var obj = {
          method: 'feed',
          redirect_uri: 'http://parajeles.com/instapi/',
          link: 'http://parajeles.com/instapi/',
          picture: $(".cboxPhoto").attr("src"),
          name: 'FID CR',
          caption: 'Puede ser via el nombre del compa',
          description: 'Puede ser la descripcion de la foto, o algo generico'
        };
		
		function callback(response) {
			console.log("posteo");
        }

        FB.ui(obj, callback);
		
		return false;
	});
	
	$(".ligthBoxGroup").colorbox({rel:'ligthBoxGroup'});
	
	instragramContainer.isotope({
        itemSelector : ".instragramPicGeneral",
  		animationEngine: 'css',
  		getSortData : {		
		  fecha : function ( $elem ) {
			return $elem.attr('fecha');
		  }
		},
		sortBy : 'fecha'
    });
 
	var instagramOptions = {
		
		accessToken:"11169779.68a296e.1eb8d06e3b9b45ae8bdb617035193ea1",
		clientId: '68a296e5a7c643a98af8a0b7ee7fa68e',
		hash:'friskyfriday',
		show : 20,
		image_size: "thumbnail",
		onComplete : imagesRender
		
	}
	
	var instagramOptionsAutomatic = {
		
		accessToken:"11169779.68a296e.1eb8d06e3b9b45ae8bdb617035193ea1",
		clientId: '68a296e5a7c643a98af8a0b7ee7fa68e',
		hash:'friskyfriday',
		show : 20,
		image_size: "thumbnail",
		onComplete : imagesRenderAutomatic
		
	}
	
	
	$(".instagram").instagram(instagramOptions);
	
	function loadFeedsAutomatic(){
		$('.waypoint').waypoint('destroy');	
		$(".instagram").instagram(instagramOptionsAutomatic);
	};
	
	function imagesRenderAutomatic(photos, data){
		creator.setIsAutomatic(true);
		creator.setLocalData(photos);
		creator.createInstragramPhotos();
	
		addWayPoint();
	};
	
	function imagesRender(photos, data){
		creator.setIsAutomatic(false);		
		creator.setFirstTime(firstTime)
		creator.setLocalData(photos);
		creator.createInstragramPhotos();
		
		if(firstTime){
			firstTime = false;
		}
		
		insta_next_url = data.pagination.next_url;
		
		addWayPoint();
		
		
	}
  	
	function loadMoreImages(direction){
		if (direction === 'down') {	
			$('.waypoint').waypoint('destroy');			
			instagramOptions.next_url = insta_next_url;	
			$(".instagram").instagram(instagramOptions);
		}	
		
	};	
	
	function addWayPoint(){
		$('.waypoint').waypoint(loadMoreImages,{offset:'110%'});
		$(".ligthBoxGroup").colorbox({rel:'ligthBoxGroup'});
	}
	
	$(".refresh").click(function(){
		window.location.href = window.location.href;
		return false;
	});
	
	$(".instragramPicGeneral").live("mouseover",
		function () {
			$(".instagramPicContainer img").addClass("grayScaleActive");
			$(this).find("img").removeClass("grayScaleActive");
		}
	);
	
	$(".instragramPicGeneral").live("mouseleave",
		function () {
			$(".instagramPicContainer img").removeClass("grayScaleActive");
		}
	);

	return false;
});
